<?php
// header("Access-Control-Allow-Origin: http://localhost:9528");

header("Access-Control-Allow-Origin: http://1.13.198.191:8081");
// 允许携带凭证
header("Access-Control-Allow-Credentials: true");
// 允许的HTTP方法
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
// 允许的请求头
header("Access-Control-Allow-Headers: Content-Type");

// 处理预检请求（OPTIONS）
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit(0);
}

require_once "./conn.php";

if ($conn->connect_error) {
    die(json_encode(["Code" => 500, "Msg" => "连接错误: " . $conn->connect_error]));
}
$data = json_decode(file_get_contents("php://input"), true);
$username = $data['username'];
$passwd = $data['password'];
$code = $data['code'];
if (isset($username) && isset($passwd)) {
    $sql1 = $conn->query("select uname from users where uname ='{$username}'"); //查询

    $row = mysqli_fetch_assoc($sql1);
    if ($row > 0) {
        echo json_encode(['Code' => 500, 'Msg' => '注册失败,账号已存在', 'Ret' => 'False', 'Data' => null]);
    } else {
        echo "没有";
        $sql = "insert into users(userid,uname,password,isadmin,code) values(?,?,?,'1',?)";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("isss", $userid, $username, $passwd, $code);
        if ($stmt->execute()) {
            echo json_encode(['Code' => 200, 'Msg' => '注册成功，请登录', 'Ret' => 'True', 'Data' => null]);

        } else {
            echo json_encode(['Code' => 500, 'Msg' => '注册失败,失败原因：' . $stmt->error, 'Ret' => 'False', 'Data' => null]);
        }
    }
}
